<template>
  <div>
    <!-- Form -->

    <el-dialog
      :title="info.isAdd ? '添加' : '修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      <el-form :model="form">
        <el-form-item label="菜单名称" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="formLabelWidth">
          <el-select
            v-model="form.pid"
            placeholder="请选择上级菜单"
            @change="changePid"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <!-- 4.6 遍历arr  -->
            <el-option
              v-for="item in arr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio v-model="form.type" :label="1" disabled>目录</el-radio>
          <el-radio v-model="form.type" :label="2" disabled>菜单</el-radio>
        </el-form-item>
        <el-form-item
          label="图标"
          :label-width="formLabelWidth"
          v-if="form.type == 1"
        >
          <el-select v-model="form.icon" placeholder="请选择图标">
            <el-option :value="item" v-for="item in icons" :key="item">
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" :label-width="formLabelWidth" v-else>
          <el-select v-model="form.url" placeholder="请选择地址">
            <el-option
              :label="i.meta.title"
              :value="'/' + i.path"
              v-for="i in IndexRoutes"
              :key="i.meta.title"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-switch
            v-model="form.status"
            inactive-text="状态"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="add">{{info.isAdd ? '添加' : '修改'}}</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { IndexRoutes } from "../../../router/index";
import { reqmenuadd, reqmenuinfo ,reqmenuedit} from "../../../http/api";
import {successAlert} from '../../../utils/alert'
export default {
  props: ["info", "arr"],
  data() {
    return {
      form: {
        pid: "",
        title: "",
        icon: "",
        type: "",
        url: "",
        status: 1,
      },

      IndexRoutes,
      icons: [
        "el-icon-s-tools",
        "el-icon-user-solid",
        "el-icon-circle-plus",
        "el-icon-remove",
        "el-icon-picture",
        "el-icon-upload",
        "el-icon-s-order",
      ],

      dialogFormVisible: true,

      formLabelWidth: "120px",
    };
  },
  mounted() {},
  methods: {
    setForm(){
    this.form = {
        pid: "",
        title: "",
        icon: "",
        type: "",
        url: "",
        status: 1,
      }
    },
    //请求列表数据
    add() {
      if(this.info.isAdd){
        
        reqmenuadd(this.form).then((res) => {
          if (res.data.code == 200) {
        //弹成功
          successAlert(res.data.msg)
          //清空form
          this.setForm();
          //弹框消失
          this.info.isshow=false;
          //列表刷新
          this.$emit('init');
          }
        });
      }else{
        reqmenuedit(this.form).then(res=>{
          if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg)
          //清空form
          this.setForm();
          //弹框消失
          this.info.isshow=false;
          //列表刷新
          this.$emit('init');
          }
        })
      }
      
    },
    //请求当前要修改的数据
    getOne(id) {
      reqmenuinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.id=id;
          
        }
      });
    },
    //根据是否是顶级菜单自动判断目录，菜单
    changePid() {
      if (this.form.pid == 0) {
        //说明是顶级菜单
        this.form.type = 1;
      } else {
        this.form.type = 2;
      }
    },
    //如果是编辑的蒙层关闭了 清空form
    close(){
      if(this.info.isAdd==false){
        this.setForm();
      }
    }
  },
  
  
};
</script>

<style>
.el-switch{
  margin-left:80px ;
}
</style>
